﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ScheduleMaker
{
    public partial class smTeachersAndHours : Form
    {
        IList<DB.Domain.Core.Teacher> teachers;
        IList<DB.Domain.Core.Day> days;
        public smTeachersAndHours()
        {
            InitializeComponent();
            grid.Rows.Clear();
            grid.DefaultCellStyle.WrapMode = DataGridViewTriState.True;
            grid.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            loadDays();
            loadTeacherHours();
        }

        private void loadTeacherHours()
        {
            DB.CoreDAO.TeacherDAO tDAO = DB.DAOFactory.getTeacherDAO(ConfigManager.ConfigManager.getConfiguration().Type);
            teachers = tDAO.getAll();
            foreach (var teacher in teachers)
            {
                int row = grid.Rows.Add();
                grid.Rows[row].Cells[0].Value = teacher.Name;
                grid.Rows[row].Height = 30;
                if (row % 2 == 1)
                {
                    for (int i = 1; i < grid.Columns.Count; i++)
                    {
                        grid.Rows[row].Cells[i].Style.BackColor = Color.FromArgb(255, 255, 204);
                    }
                }
                IList<DB.Domain.Core.Hour> teacherHours = teacher.Hours;
                foreach (var teacherHour in teacherHours)
                {
                    for (int column = 1; column < grid.ColumnCount; column++)
                    {
                        if (grid.Columns[column].Name.Equals(teacherHour.Day.Name))
                        {
                            TimeSpan tsBegin = teacherHour.Begin;
                            TimeSpan tsEnd = teacherHour.End;
                            String b = "";
                            String e = "";
                            if (tsBegin.Minutes.Equals(0))
                            {
                                b = tsBegin.Hours.ToString() + ":00";
                            }
                            else
                            {
                                b = tsBegin.Hours.ToString() + ":" + tsBegin.Minutes.ToString();
                            }
                            if (tsEnd.Minutes.Equals(0))
                            {
                                e = tsEnd.Hours.ToString() + ":00";
                            }
                            else
                            {
                                e = tsEnd.Hours.ToString() + ":" + tsEnd.Minutes.ToString();
                            }
                            String time = b + "-" + e;
                            if (grid.Rows[row].Cells[column].Value != null)
                            {
                                grid.Rows[row].Height = grid.Rows[row].Height + 15;
                                grid.Rows[row].Cells[column].Value = grid.Rows[row].Cells[column].Value + "\n" + time;
                            }
                            else
                            {
                                grid.Rows[row].Cells[column].Value = time;
                            }

                        }
                    }
                }
            }
        }
        private void loadDays()
        {
            //Wczytywanie Dni
            DB.CoreDAO.DayDAO dDao = DB.DAOFactory.getDayDAO(ConfigManager.ConfigManager.getConfiguration().Type);
            days = dDao.getAll();
            grid.ColumnCount = days.Count + 1;
            grid.Columns[0].Frozen = true;
            grid.Columns[0].Width = 150;
            System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle();

            dataGridViewCellStyle1.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
            dataGridViewCellStyle1.BackColor = System.Drawing.SystemColors.Control;
            dataGridViewCellStyle1.Font = new System.Drawing.Font("Verdana", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(238)));
            dataGridViewCellStyle1.ForeColor = System.Drawing.SystemColors.WindowText;
            dataGridViewCellStyle1.SelectionBackColor = System.Drawing.SystemColors.Highlight;
            dataGridViewCellStyle1.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
            dataGridViewCellStyle1.WrapMode = System.Windows.Forms.DataGridViewTriState.True;
            grid.Columns[0].DefaultCellStyle = dataGridViewCellStyle1;

            int i = 1;
            foreach (var day in days)
            {
                grid.Columns[i].Name = day.Name;
                grid.Columns[i].Width = 150;
                i++;
            }
        }
    }

}
